// Initialize app
var myApp = new Framework7({
    //template7Pages: true, //enable Template7 rendering for pages
    //Specify templates/pages data
    precompileTemplates: true,
    template7Pages: true,
});
// If we need to use custom DOM library, let's save it to $$ variable:
var $$ = Framework7.$;

// Add view
var mainView = myApp.addView('.view-main', {
    // Because we want to use dynamic navbar, we need to enable it for this view:
    dynamicNavbar: true,
})

var currentPage = 1;
var alreadyAlert = true;
var queryParas = {
    PageSize: 10,
    PageNumber: 1,
}
$$(document).on('pageAfterAnimation', function (e) {
    App.pageAfterAnimation(e.detail.page.name);
});

$$(document).on('pageInit', function (e) {
    // Get page data from event data
    var page = e.detail.page;
    if (page.name === 'index') {
        onEvent();
        addSearchResult(queryParas, "refresh");
    }else if(page.name === 'seller-shop'){
        App.loadSellerShop();
    } else if (page.name === 'detail') {
        App.loadDetailPage();
    }else if(page.name === 'reserve'){
        var typeId =App.judgeType($("#detail-typeId").val());
        if(typeId == 23 || typeId == 13){
            App.loadTicketReservePage();
        }else if(typeId == 24 || typeId == 15){
            App.loadCharteredReservePage();
        }
    }
})

function onEvent() {
    var para = {
        PageSize: 10,
        PageNumber: 1,
    };
    //上拉无限加载
    var loading = false;
    // 添加'infinite'监听器
    $$('.infinite-scroll').on('infinite', function () {
        if (loading) return;
        // 设置flag
        loading = true;
        currentPage = currentPage + 1;
        para.PageNumber = currentPage;
        addSearchResult(para);
        loading = false;
    });

    // 添加'refresh'监听器
    $$('.pull-to-refresh-content').on('refresh', function (e) {
        para.PageNumber = 1;
        para.PageSize = 10;
        if (!alreadyAlert) {
            alreadyAlert = true;
            var html = '<div class="infinite-scroll-preloader">' +
                '<div class="preloader" style="margin-left:48%"></div>' +
                '</div>';
            $$('.page-content').append(html);
        }
        $$('div[name=items]').remove();
        addSearchResult(para, "refresh")
        myApp.pullToRefreshDone();
    });
}

var totalPage = 1;
function addSearchResult(para, AddType) {
    if (para.PageNumber > totalPage) {
        if (alreadyAlert) {
            Constant.alertNotification( "往下已没有更多内容了，刷新试试！","新西兰微助手提醒您");
            $$('.infinite-scroll-preloader').remove();
            alreadyAlert = false;
        }
        return false;
        App.hideLoading();
    }
    $.ajax({
        url: Constant.ctx("/mulanweb/wechat/collection/search"),
        async: false,
        type: "post",
        dataType: "json",
        data: para,
        success: function (info) {
            if (info.list ) {
                var template = $('#searchDataList').html();
                var compiledTemplate = Template7.compile(template);
                var htmlStr = compiledTemplate(info);
                if (AddType) {
                    $("#queryResultList").html(htmlStr);
                    currentPage = 1;
                } else {
                    $("#queryResultList").append(htmlStr);
                }
                if(info.TotalPage <=info.PageNumber  || (!info.TotalPage)){
                    $$('.infinite-scroll-preloader').remove();
                }
                totalPage = data.TotalPage;
            }else{
                if(info.TotalPage <=info.PageNumber || (!info.TotalPage)){
                    $$('.infinite-scroll-preloader').remove();
                }
            }
            App.hideLoading();
        }
    }, para, function (data) {
    });
}
